package fit.hrkj.eureka.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * 向服务的通过这者  , 发起请求 , 得到数据
 *
 * @author 刘二朋
 * @date 2020-12-30 16:44
 */
@RestController
@Configuration
public class AskController {

    //    注入本地服务名
    @Value("${spring.application.name}")
    private String name;

    //    自动注册一个restTemplate
    @Autowired
    RestTemplate restTemplate;

    //    生成实例
    @Bean
//    负载均衡
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @RequestMapping("/ask")
    public String ask() {

//        调用服务提供者的代码
        String body = restTemplate.getForObject("http://EUREKA-PROVIDER/eureka-provider/hello/{name}", String.class, name);
        return body;

    }


}
